package io.gitee.zhangbinhub.acp.boot.socket.base

import io.gitee.zhangbinhub.acp.boot.socket.conf.SocketListenerConfiguration
import io.netty.channel.ChannelHandlerContext

/**
 * Socket 报文处理接口
 */
abstract class SocketServerHandle {
    private var socketListenerConfiguration: SocketListenerConfiguration? = null

    /**
     * 对接收到的报文进行处理
     *
     * @param receiveStr 接收到的报文
     * @return 返回报文
     */
    @Throws(Exception::class)
    abstract fun doProcess(receiveStr: String): String

    /**
     * 事件触发
     *
     * @param event 事件 e.g. IdleStateEvent
     * @return 发送内容
     * @throws Exception 异常
     */
    @Throws(Exception::class)
    abstract fun userEventTriggered(event: Any): String?

    /**
     * 异常捕获
     * @param context 通讯上下文
     * @param cause 异常信息
     */
    abstract fun exceptionCaught(context: ChannelHandlerContext, cause: Throwable)

    /**
     * 设置配置参数
     * @param socketListenerConfiguration 监听配置参数
     */
    fun setSocketListenerConfiguration(socketListenerConfiguration: SocketListenerConfiguration) {
        this.socketListenerConfiguration = socketListenerConfiguration
    }

    /**
     * 获取配置参数
     */
    fun getSocketListenerConfiguration(): SocketListenerConfiguration? = socketListenerConfiguration
}
